
package com.yimixian.app.receiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;

import com.igexin.sdk.PushConsts;
import com.yimixian.app.R;
import com.yimixian.app.SystemFramework;
import com.yimixian.app.activity.YMXMainActivity;
import com.yimixian.app.model.GeTuiPushModel;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by songzy on 2015/12/22.
 */
public class GeTuiPushReceiver extends BroadcastReceiver {
    public static final int REQUEST_CODE_NORMAL = 99;
    public static final String TRANSMITTED_MESSAGE_EXTRA= "transmitted_message_extra";
    public static final String TRANSMITTED_MESSAGE_TYPE = "transmitted_message_type";
    public static final int TRANSMITTED_MESSAGE_TYPE_URL = 0;

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if(bundle == null) {
            return;
        }
        switch (bundle.getInt(PushConsts.CMD_ACTION)) {
            case PushConsts.GET_MSG_DATA:
                byte[] title = bundle.getByteArray("payload");
                if (title != null)
                {
                    String result = new String(title);
                    JSONObject jsonObject = null;
                    if(!TextUtils.isEmpty(result)) {
                        try {
                            jsonObject = new JSONObject(result);
                        } catch (JSONException e) {
                            e.printStackTrace();
                            return ;
                        }
                    }
                    GeTuiPushModel model = new GeTuiPushModel();

                    if(jsonObject != null) {
                        model.title = jsonObject.optString("title").trim();
                        model.content = jsonObject.optString("content").trim();
                        model.url = jsonObject.optString("url").trim();
                    }
                    NotificationManager notificationManager = (NotificationManager)SystemFramework.getInstance().getGlobalContext().getSystemService(Context.NOTIFICATION_SERVICE);
                    NotificationCompat.Builder builder = new NotificationCompat.Builder(SystemFramework.getInstance().getGlobalContext()).setSmallIcon(R.mipmap.ic_launcher).setDefaults(Notification.DEFAULT_ALL);
                    if(model != null && !TextUtils.isEmpty(model.title)) {
                        builder.setContentTitle(model.title);
                    }
                    if(model != null && !TextUtils.isEmpty(model.content)) {
                        builder.setContentText(model.content);
                        builder.setTicker(model.content);
                    }
                    builder.setAutoCancel(true);

                    Intent intent1 = new Intent(SystemFramework.getInstance().getGlobalContext(), YMXMainActivity.class);
                    if(model != null && !TextUtils.isEmpty(model.url)) {
                        intent1.putExtra(TRANSMITTED_MESSAGE_EXTRA, model.url);
                        intent1.putExtra(TRANSMITTED_MESSAGE_TYPE, TRANSMITTED_MESSAGE_TYPE_URL);
                    }
                    intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    PendingIntent pendingIntent = PendingIntent.getActivity(SystemFramework.getInstance().getGlobalContext(),REQUEST_CODE_NORMAL,intent1,PendingIntent.FLAG_ONE_SHOT);
                    builder.setContentIntent(pendingIntent);
                    Notification notify = builder.build();
                    if(Build.VERSION.SDK_INT < 16) {
                        notify.flags = Notification.FLAG_AUTO_CANCEL;
                    }
                    notificationManager.notify(0,notify);
                }
                break;
        }
    }
}
